<?php

namespace App\Http\Model;

use Illuminate\Database\Eloquent\Model;

class Category extends Model
{
    protected $table='category';
    protected $primaryKey='cate_id';
    public $timestamps=false;
    protected $guarded=[];
    public function tree()
    {
        $category=$this->orderBy('cate_order','asc')->get();
        return $this->getTree($category);
    }

    public function getTree($data,$id=0)
    {
        $tree=array();
        foreach ($data as $k=>$v){
            if($v->cate_pid==$id){
                $data[$k]['_cate_name']=$data[$k]['cate_name'];
                $tree[]=$data[$k];
                foreach ($data as $m=>$n){
                    if($n->cate_pid ==$v->cate_id){
                        $data[$m]['_cate_name']='├━ '.$data[$m]['cate_name'];
                        $tree[]=$data[$m];
                    }
                }
            }
        }
        return $tree;
    }

}
